package com.asiainfo.cms.system.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.asiainfo.cms.common.constant.UserConstants;
import com.asiainfo.cms.common.exception.BizException;
import com.asiainfo.cms.system.entity.SysDictData;
import com.asiainfo.cms.system.entity.SysDictType;
import com.asiainfo.cms.system.mapper.SysDictDataMapper;
import com.asiainfo.cms.system.mapper.SysDictTypeMapper;
import com.asiainfo.cms.system.service.ISysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService {
    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init() {
        loadingDictCache();
    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public Page<SysDictType> selectDictTypeList(Page page,SysDictType dictType) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(dictType.getDictName())) {
            wrapper.like("dict_name", dictType.getDictName());
        }
        if (ObjectUtil.isNotEmpty(dictType.getDictType())) {
            wrapper.like("dict_type", dictType.getDictType());
        }
        if (ObjectUtil.isNotEmpty(dictType.getDictId())) {
            wrapper.like("dict_id", dictType.getDictId());
        }
        return dictTypeMapper.selectPage(page,wrapper);
    }

    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeAll() {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();

        return dictTypeMapper.selectList(wrapper);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictType);
        List<SysDictData> dictDatas = dictDataMapper.selectList(wrapper);
        if (ArrayUtil.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        return dictDatas;
    }

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return dictTypeMapper.selectById(dictId);
    }

    /**
     * 根据字典类型查询信息
     *
     * @param dictType 字典类型
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeByType(String dictType) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictType);
        return dictTypeMapper.selectOne(wrapper);
    }

    /**
     * 批量删除字典类型信息
     *
     * @param dictIds 需要删除的字典ID
     */
    @Override
    public void deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds) {
//            SysDictType dictType = selectDictTypeById(dictId);
//            QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
//            wrapper.eq("dict_type", dictType.getDictType());
//            List<SysDictType> list = dictTypeMapper.selectList(wrapper);
//            if (list.size() > 0) {
//                throw new BizException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
//            }
            dictTypeMapper.deleteById(dictId);
        }
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache() {
        SysDictData dictData = new SysDictData();
        dictData.setStatus("0");
    }


    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        loadingDictCache();
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dict 字典类型信息
     * @return 结果
     */
    @Override
    public int insertDictType(SysDictType dict) {
        return dictTypeMapper.insert(dict);
    }

    /**
     * 修改保存字典类型信息
     *
     * @param dict 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDictType(SysDictType dict) {
        return dictTypeMapper.updateById(dict);
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public String checkDictTypeUnique(SysDictType dict) {
        Long dictId = ObjectUtil.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", dictId);
        wrapper.eq("dict_type", dict.getDictType());
        SysDictType dictType = dictTypeMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
